package com.chinawayltd.ews.sample.web.basicInfo;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.chinawayltd.ews.sample.core.exception.BusinessException;
import com.chinawayltd.ews.sample.model.TDictInfo;
import com.chinawayltd.ews.sample.model.TDictType;
import com.chinawayltd.ews.sample.model.basicInfo.DictInfoVO;
import com.chinawayltd.ews.sample.service.basicInfo.DictService;
import com.chinawayltd.ews.sample.util.BaseController;
import com.chinawayltd.ews.sample.util.ResultModel;
import com.chinawayltd.ews.sample.util.StringUtils;
import com.chinawayltd.ews.sample.web.query.DictTypeGirdQuery;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @program: greeProject
 * @description 字典类型及字典项
 * @author: chenxin
 * @create: 2018年5月11日 下午10:43:27
 */

@Api(tags = { "字典类型及字典项API" })
@RestController
@RequestMapping(value = "/web/dict")
public class DictController extends BaseController{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	DictService dictService;
	
	@ApiOperation(value = "列表查询接口")
	@RequestMapping(value = "/loadGirdData", method = { RequestMethod.POST })
	public ResultModel loadGirdData(DictTypeGirdQuery queryParam) {
		ResultModel model = new ResultModel();
		try {
			List<TDictType> list = dictService.listDictType(queryParam);
			model.setCode(0);
			model.setSub_code(0);
			model.setData(new JSONObject().fluentPut("result", list));
			model.setPage(queryParam);
		} catch (Exception e) {
			model.setSub_code(1);
			model.setData(new JSONObject().fluentPut("sub_msg", null));
			logger.error("列表查询出错！", e);
		}
		return model;
	}

	@ApiOperation(value = "字典类型信息根据ID查询接口")
	@RequestMapping(value = "/getDictTypeById/{id}", method = { RequestMethod.GET })
	@ApiImplicitParam(paramType = "path", name = "id", value = "道路限行管理ID", dataType = "Long", required = true)
	public ResultModel getDictTypeById(@PathVariable Long id) {
		ResultModel model = new ResultModel();
		try {
            TDictType data = dictService.getDictTypeById(id);
            model.setData(new JSONObject().fluentPut("result" , data));
        }catch (Exception e){
        	model.setSub_code(1);
			model.setData(new JSONObject().fluentPut("sub_msg" , "查询失败"));
            logger.error("字典类型信息根据ID查询出错！id：" + id, e);
        }
        return new ResultModel(0, 0, new JSONObject().fluentPut("sub_msg" , null));
	}

	@ApiOperation(value = "字典类型信息新增接口")
	@RequestMapping(value = "/addDictType", method = { RequestMethod.POST })
	public ResultModel addDictType(TDictType tDictType) {
		ResultModel model = new ResultModel();
		try {
			if(StringUtils.isBlank(tDictType.getCode())){
				throw new BusinessException("添加失败");
			}
			TDictType dict = dictService.isHasDictTypeByIdNO((Long) null, tDictType.getCode());
			if(dict !=null){throw new BusinessException("编号已存在，添加失败");}
			int result = dictService.addDictType(tDictType);
			if(result > 0){
	        	model.setData(new JSONObject().fluentPut("result" , "添加成功"));
	        }else{
	        	model.setSub_code(1);
	        	model.setData(new JSONObject().fluentPut("sub_msg" , "添加失败"));
	        }
		} catch (BusinessException e) {
			model.setSub_code(1);
			model.setData(new JSONObject().fluentPut("sub_msg", e.getMessage()));
		} catch (Exception e) {
			model.setSub_code(1);
			model.setData(new JSONObject().fluentPut("sub_msg" , "添加失败"));
			logger.error("字典类型信息新增发生异常！", e);
		}
        return model;
	}
	
	@ApiOperation(value = "字典项信息新增接口")
	@RequestMapping(value = "/addDictInfo", method = { RequestMethod.POST })
	public ResultModel addDictInfo(TDictInfo tDictInfo) {
		ResultModel model = new ResultModel();
		try {
			if(tDictInfo.getTypeId() == null || tDictInfo.getTypeId() <= 0){
				 throw new RuntimeException("字典类型不能为空");
	        }
			int result = dictService.addDictInfo(tDictInfo);
			if(result > 0){
	        	model.setData(new JSONObject().fluentPut("result" , "添加成功"));
	        }else{
	        	model.setSub_code(1);
	        	model.setData(new JSONObject().fluentPut("sub_msg" , "添加失败"));
	        }
		} catch (Exception e) {
			model.setSub_code(1);
	    	model.setData(new JSONObject().fluentPut("sub_msg" , "添加失败"));
			logger.error("字典项信息新增发生异常！", e);
		}
        return model;
	}

	@ApiOperation(value = "根据字典类型查询字典项接口")
	@RequestMapping(value = "/listDictInfoByTypeId/{typeId}", method = { RequestMethod.GET })
	@ApiImplicitParam(paramType = "path", name = "typeId", value = "字典类型ID", dataType = "Long", required = true)
	public ResultModel listDictInfoByTypeId(@PathVariable Long typeId) {
		ResultModel model = new ResultModel();
		try {
            List<DictInfoVO> list = dictService.listDictInfoByTypeId(typeId);
			model.setCode(0);
			model.setSub_code(0);
			model.setData(new JSONObject().fluentPut("result", list));
        }catch (Exception e){
        	model.setSub_code(1);
	    	model.setData(new JSONObject().fluentPut("sub_msg" , "查询失败"));
            logger.error("根据字典类型查询字典项出错！id：" + typeId, e);
        }
        return model;
	}
	
	@ApiOperation(value = "字典类型信息修改接口")
	@RequestMapping(value = "/updateDictType", method = { RequestMethod.POST })
	public ResultModel updateDictType(TDictType tDictType) {
		ResultModel model = new ResultModel();
		try {
			if(StringUtils.isBlank(tDictType.getId().toString()) || StringUtils.isBlank(tDictType.getCode())){
				throw new BusinessException("修改失败！");
			}
			TDictType dict = dictService.isHasDictTypeByIdNO(tDictType.getId(), tDictType.getCode());
			if(dict !=null){
				throw new BusinessException("编号已存在，修改失败");
			}
			int result = dictService.updateDictType(tDictType);
	        if(result > 0){
	        	model.setData(new JSONObject().fluentPut("result" , "修改成功"));
	        }else{
	        	model.setSub_code(1);
	        	model.setData(new JSONObject().fluentPut("sub_msg" , "修改失败"));
	        }
		} catch (BusinessException e) {
			model.setSub_code(1);
			model.setData(new JSONObject().fluentPut("sub_msg", e.getMessage()));
		} catch (Exception e) {
			model.setSub_code(1);
	    	model.setData(new JSONObject().fluentPut("sub_msg" , "修改失败"));
			logger.error("字典类型信息修改发生异常！！", e);
		}
        return model;
	}
	@ApiOperation(value = "字典项信息修改接口")
	@RequestMapping(value = "/updateDictInfo", method = { RequestMethod.POST })
	public ResultModel updateDictInfo(TDictInfo tDictInfo) {
		ResultModel model = new ResultModel();
		try {
			if(StringUtils.isBlank(tDictInfo.getId().toString()) || StringUtils.isBlank(tDictInfo.getTypeId().toString())){
				throw new BusinessException("修改失败！");
			}
			int result = dictService.updateDictInfo(tDictInfo);
			if(result > 0){
	        	model.setData(new JSONObject().fluentPut("result" , "修改成功"));
	        }else{
	        	model.setSub_code(1);
	        	model.setData(new JSONObject().fluentPut("sub_msg" , "修改失败"));
	        }
	    } catch (BusinessException e) {
			model.setSub_code(1);
			model.setData(new JSONObject().fluentPut("sub_msg", e.getMessage()));
		} catch (Exception e){
	    	model.setSub_code(1);
	    	model.setData(new JSONObject().fluentPut("sub_msg" , "修改失败"));
	    	model.setSub_msg(e.getMessage());
	    	logger.error("字典项信息修改发生异常！"+e);
	    }
		return model;
	}
}
